package com.hz.monitoring.module.hardware.dal.mysql.sensor_tenant_type;

import cn.hutool.core.collection.CollectionUtil;
import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.mybatis.core.mapper.BaseMapperX;
import com.hz.monitoring.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_type.vo.SensorTenantTypeCreateReqVO;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_type.vo.SensorTenantTypeExportReqVO;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_type.vo.SensorTenantTypeListReqVO;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_type.vo.SensorTenantTypePageReqVO;
import com.hz.monitoring.module.hardware.convert.sensor_tenant_type.SensorTenantTypeConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.sensor_tenant_type.SensorTenantTypeDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 传感器租户分类 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface SensorTenantTypeMapper extends BaseMapperX<SensorTenantTypeDO> {

    default PageResult<SensorTenantTypeDO> selectPage(SensorTenantTypePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SensorTenantTypeDO>()
                .eqIfPresent(SensorTenantTypeDO::getMcuModel, reqVO.getMcuModel())
                .eqIfPresent(SensorTenantTypeDO::getModel, reqVO.getModel())
                .eqIfPresent(SensorTenantTypeDO::getTenantId, reqVO.getTenantId())
                .betweenIfPresent(SensorTenantTypeDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(SensorTenantTypeDO::getId));
    }

    default Boolean createBatchSensorTenantType(List<SensorTenantTypeCreateReqVO> createReqVO) {
        List<SensorTenantTypeDO> list = selectList(new LambdaQueryWrapperX<SensorTenantTypeDO>()
                .eqIfPresent(SensorTenantTypeDO::getTenantId, createReqVO.get(0).getTenantId())
                .eqIfPresent(SensorTenantTypeDO::getMcuModel,createReqVO.get(0).getMcuModel())
        );

        Map<String, SensorTenantTypeCreateReqVO> sensorSysTypeIdByData = createReqVO.stream().collect(Collectors.toMap(SensorTenantTypeCreateReqVO::getModel, a -> a, (k1, k2) -> k1));
        Map<String, SensorTenantTypeDO> sensorSysTypeIdBySensorTenantTypeDO = list.stream().collect(Collectors.toMap(SensorTenantTypeDO::getModel, a -> a, (k1, k2) -> k1));

        //获取要删除的数据
        List<Long> deleteIds = list.stream().filter(it -> !sensorSysTypeIdByData.containsKey(it.getModel())).map(it -> it.getId()).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(deleteIds)) {
            deleteBatchIds(deleteIds);
        }

        //获取要添加的数据
        createReqVO=  createReqVO.stream().filter(it -> !sensorSysTypeIdBySensorTenantTypeDO.containsKey(it.getModel())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(createReqVO)) {
            List<SensorTenantTypeDO> mcuTenantTypeList = SensorTenantTypeConvert.INSTANCE.convert(createReqVO);
            insertBatch(mcuTenantTypeList);
        }
        return true;
    }

    default List<SensorTenantTypeDO> selectList(SensorTenantTypeListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<SensorTenantTypeDO>()
                .eqIfPresent(SensorTenantTypeDO::getMcuModel, reqVO.getMcuModel())
                .eqIfPresent(SensorTenantTypeDO::getModel, reqVO.getModel())
                .eqIfPresent(SensorTenantTypeDO::getTenantId, reqVO.getTenantId())
                .inIfPresent(SensorTenantTypeDO::getId, reqVO.getIds())
                .betweenIfPresent(SensorTenantTypeDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(SensorTenantTypeDO::getId));
    }


    default List<SensorTenantTypeDO> selectList(SensorTenantTypeExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<SensorTenantTypeDO>()
                .eqIfPresent(SensorTenantTypeDO::getMcuModel, reqVO.getMcuModel())
                .eqIfPresent(SensorTenantTypeDO::getModel, reqVO.getModel())
                .eqIfPresent(SensorTenantTypeDO::getTenantId, reqVO.getTenantId())
                .betweenIfPresent(SensorTenantTypeDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(SensorTenantTypeDO::getId));
    }

}
